package com.condominio.servlet.auth;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.condominio.facade.Facade;
import com.condominio.model.Pessoa;


public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Facade fachada= Facade.getInstance();


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pessoa pes=new Pessoa();
		String login=request.getParameter("usu_login");
		String senha=request.getParameter("usu_senha");
		
		pes.setLogin(login);
		pes.setSenha(senha);
		if(this.loga(pes)){
			HttpSession s=request.getSession();
			pes=fachada.findPessoa(pes);
			s.setAttribute("Usuario", pes);
			response.sendRedirect("aplicacao/main.jsp");
			return;
		}
		else{
			response.sendError(401);
			return;
		}

		
	}

	
	public boolean loga(Pessoa p){
		return fachada.login(p);
	}

}
